﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PercolationTree.Model
{
    class TreeFactory
    {
        private static Random m_Random = new Random();
        public static Tree BuildDeterminedTree(int childrenPerNode, int totalLayers)
        {
            Tree tree = new Tree();

            while (tree.Layers.Count < totalLayers)
            {
                Layer layer = tree.Layers.Last();
                foreach (Node node in layer.Nodes)
                    for (int i = 0; i < childrenPerNode; i++)
                        tree.AddChildNode(node, new Node());
            }

            return tree;
        }

        public static Tree BuildRandomTree(int maxChildrenPerNode, int maxTotalLayers)
        {
            Tree tree = new Tree();

            int totalLayers = m_Random.Next(1, maxTotalLayers);
            int childPerNode;
            while (tree.Layers.Count < totalLayers)
            {
                Layer layer = tree.Layers.Last();
                foreach (Node node in layer.Nodes)
                {
                    childPerNode = m_Random.Next(1, maxChildrenPerNode);
                    for (int i = 0; i < childPerNode; i++)
                        tree.AddChildNode(node, new Node());
                }
            }

            return tree;
        }

        public static Tree BuildFixedTree(int childrenPerNode, int totalNodes)
        {
            Tree tree = new Tree();

            while (true)
            {
                Layer layer = tree.Layers.Last();
                foreach (Node node in layer.Nodes)
                    for (int i = 0; i < childrenPerNode; i++)
                    {
                        if (totalNodes <= 1)
                            return tree;
                        tree.AddChildNode(node, new Node());
                        totalNodes--;
                    }
            }
        }
    }
}
